<?php

namespace App\Http\Controllers;

use App\Models\ActionLogModel;
use App\Services\LoginAccountService;
use App\Services\LoginService;
class LoginController extends Backend
{
    protected $loginAccountService;

    public function __construct()
    {
        parent::__construct();
        $this->service = new LoginService();
        $this->loginAccountService = new LoginAccountService();
    }

    /**
     * @OA\Get(
     *     path="/captcha",
     *     operationId="login_captcha",
     *     tags={"Captcha"},
     *     summary="获取登录验证码",
     *     description="获取登录验证码",
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function captcha()
    {
        $result = $this->service->captcha();
        return $result;
    }

    /**
     * @OA\Post(
     *     path="/login",
     *     operationId="login_login",
     *     tags={"Login"},
     *     summary="用户登录",
     *     description="用户登录",
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"username", "password"},
     *                 @OA\Property(property="username", description="用户名", type="string"),
     *                 @OA\Property(property="password", description="密码", type="string"),
     *                 @OA\Property(property="platform", description="客户端平台(android、ios)", type="string"),
     *                 @OA\Property(property="cid", description="登录设备ID", type="string")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function login()
    {
        $result = $this->service->login();
        return $result;
    }

    /**
     * @OA\Post(
     *     path="/login/account",
     *     operationId="login_account",
     *     tags={"Login"},
     *     summary="账号登录",
     *     description="账号登录(注意: 该token仅用于前台账号登录，后台无法通过验证)",
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"account", "verify_code"},
     *                 @OA\Property(property="account", description="登录账号（暂时仅支持手机号码）", type="string"),
     *                 @OA\Property(property="verify_code", description="验证码（暂时仅支持手机短信验证码）", type="string")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function account()
    {
        try {
            $data = $this->loginAccountService->login();
            return message("登录成功", true, $data);
        } catch (\Exception $e) {
            return message("登录失败(" . $e->getMessage() . ")", false);
        }
    }

    public function logout()
    {
        $result = $this->service->logout();
        return $result;
    }

}
